package com.zhiwei.tool;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhiwei.mapper.UserMapper;
import com.zhiwei.pojo.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@NoArgsConstructor
@Data
class UserMsgUtil {
    public static final Integer USER_TYPE_FINANCE = 1;
    public static final Integer USER_TYPE_PERSONNEL = 2;
    public static final Integer USER_TYPE_STORES = 3;

    @Autowired
    private UserMapper userMapper;

    public UserMapper getUserMapper(){
        return userMapper;
    }

    @Resource
    public void setUserMapper(UserMapper userMapper){
        this.userMapper=userMapper;
    }

    public int addMsgNum(Integer userId){
        User user = userMapper.selectById(userId);
        Integer leaderId = user.getLeaderId();
        User leader = userMapper.selectById(leaderId);
        leader.setMsgNum(leader.getMsgNum()+1);
        return userMapper.updateById(leader);
    }

    public int addMsgByType(Integer userType){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("ismanager",userType);
        List<User> users = userMapper.selectList(wrapper);
        int i = 0;
        for (User user : users) {
            user.setMsgNum(user.getMsgNum()+1);
            userMapper.updateById(user);
            i++;
        }
        return i;
    }
    public int cleanMsgNum(Integer userId){
        log.info("清空信息"+userId);
        User user = userMapper.selectById(userId);
        user.setMsgNum(0);
        return userMapper.updateById(user);
    }
}
